<template>
  <div>
      <el-form ref="article" :rules="rules" :model="article"  label-width="80px">
    <el-form-item label="标题" prop="title">
      <el-input  v-model='article.title'  placeholder="请输入标题"/>
    </el-form-item>

    <el-form-item label="内容" prop='articleBody' ref="formItem">
      <quill-editor
      v-model="article.articleBody"
      :options="editorOption"
      @blur="hBlur"
      ></quill-editor>
    </el-form-item>

    <el-form-item label="视频地址">
      <el-input v-model="article.videoURL" placeholder="请输入标题"/>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hConfirm">确 定</el-button>
      <el-button @click="hClose">取 消</el-button>
    </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
import { detail, update, add } from '@/api/hmmm/articles.js'
export default {
  name: 'addArticle',
  components: {
    quillEditor
  },
  props: ['id'], // 父组件传入的要编辑的id
  data () {
    return {
      // 规则
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      // 文章详情
      article: {
        articleBody: '', // 内容
        createTime: '', // 时间
        creatorID: '',
        id: '',
        state: '',
        title: '', // 标题
        videoURL: null, // 视频路径
        visits: ''
      }
    }
  },
  // 创建完毕
  created () {
    // 传入id，说明是编辑，否则是添加
    if (this.id) {
      this.loadDetail()
    }
  },
  methods: {
    // 在编辑情况下，获取详情
    async loadDetail () {
      try {
        const { data: res } = await detail({ id: this.id })
        console.log(res)
        this.article = res
      } catch (err) {
        console.log(err)
      }
    },
    // 富文本框失去了焦点
    hBlur () {
      console.log('富文本框失去了焦点')
      // 手动去触发对应的el-form-item项去验证
      //  this.$refs.formItem : 找到要验证的那个form-item
      //  validate是form-item对象上的方法，
      //  validate('blur'): 验证失焦的情况
      this.$refs.formItem.validate('blur')
    },
    // 确认按钮
    hConfirm () {
      // 手动去做一次表单校验
      this.$refs.article.validate(valid => {
        console.log(valid)
        if (valid) {
          // 如果有id说明是编辑，做doUpdate
          this.id ? this.doUpdate() : this.doAdd()
        }
      })
    },
    // 关闭按钮
    hClose () {
      this.$emit('close')
    },
    // 做添加
    async doAdd () {
      try {
        await add({
          title: this.article.title,
          articleBody: this.article.articleBody,
          videoURL: this.article.videoURL
        })
      } catch (err) {
        console.log(err)
      }
      // 子传父重铺页面关闭弹窗
      this.$emit('addorEdit')
    },
    // 做编辑
    async doUpdate () {
      console.log('编辑')
      const data = {
        id: this.article.id,
        title: this.article.title,
        articleBody: this.article.articleBody,
        videoURL: this.article.videoURL
      }
      await update(data)
      // 子传父重铺页面关闭弹窗
      this.$emit('addorEdit')
    }
  }
}
</script>

<style lang='css'>
  .ql-editor{
     min-height:200px;
    }
</style>
